<?php

/*
 * This class is used for as Model for Job table 
 */

class Model_Job extends Model_Base {

    protected $_name = 'tbl_job';
    protected $_primaryKey = 'job_id';

    /**
     * Function to return different units of the Job
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getUnitArray() {
        //Load UOM Model
        $uomModel = new Model_Uom();
        $uomArray = $uomModel->fetchUomArray();
        return $uomArray; 
    }
    
    /**
     * Function to return different status of the Job
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            'active' => 'Active',
            'inactive' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('job' => 'tbl_job'))
                ->where('job_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get jobs
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchJobs($where = "", $limit = "", $offset = "0", $order = 'job_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('job' => 'tbl_job'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getJobCount($where) {
        $select = $this->select()
                ->from(array('job' => 'tbl_job'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete jobs
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteJob($jobtArray) {
        $loginModel = new Model_Login();
        if (is_array($jobtArray)) {
            foreach ($jobtArray as $_job) {
                $jobData = Array();
                $jobData = $this->fetchEntryById($_job);
                if (count($jobData) > 0) {
                    $this->delete('job_id = ' . (int) $_job);
                }
            }
        } else {
            $job_id = $jobtArray;
            $jobData = $this->fetchEntryById($job_id);
            if (count($jobData) > 0) {
                $this->delete('job_id = ' . (int) $job_id);
            }
        }
    }

}
